package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CoursePubService;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;
    @Autowired
    private CoursePubService coursePubService;

    /*
    SpringMVC对于参数的处理默认使用的QueryString形式，如果使用其他传入参数形式，需要通过注解来表示
     */
    @PostMapping("course/list")
    public PageVO queryCourseBaseList(PageRequestParams params,
                                      @RequestBody QueryCourseBaseModel model) {
       //1.获取访问令牌并从中解析出机构的信息ID数据
        Long companyId = UAASecurityUtil.getCompanyId();
        // 2.调用service层的方法
      PageVO<CourseBaseDTO>pageVo= courseBaseService.queryCourseBaseList(params,model,companyId);
        return pageVo;
    }

    //新增信息数据
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.createCourseBase(courseBaseDTO);
    }

    //通过id获得基础信息
    @GetMapping(value = "course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        //1.获得公司Id值
        Long companyId = UAASecurityUtil.getCompanyId();
        //2.获得课程基本信息数据
        CourseBaseDTO courseBase = courseBaseService.getByCourseBaseId(courseBaseId,companyId);
        return courseBase;
    }

    //更新基础信息
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //1.将VO数据转为DTO数据
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        //2.通过工具类获得公司和用户信息
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.modifyCourseBase(courseBaseDTO);
    }

    //删除基础信息
    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courseBaseId,companyId);
    }

    //课程基本信息审核
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId,companyId);
    }

    @GetMapping("course/preview/{courseId}/{companyId}")
    public Object previewCourse(@PathVariable Long courseId, @PathVariable Long companyId) {
        Map<String,Object> dataMap =courseBaseService.previewCourse(courseId, companyId);
        //1.使用师父和数据模型对象
        ModelAndView modelAndView = new ModelAndView("learing_article", dataMap);
        return modelAndView;
    }

    //课程发布
    @PostMapping("course_pub/publish/{courseBaseId}")
    public void publish(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        coursePubService.publish(courseBaseId,companyId,false);

    }


}
